/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphic;

import exceptions.CaminhoIncorreto;
import exceptions.IndiceInvalido;
import exceptions.NaoEhDiretorio;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import main.BuscaArquivo;

/**
 *
 * @author mewki
 */
public class BuscaArquivoGUI extends javax.swing.JFrame {

    /**
     * Creates new form BuscaArquivo
     */
    public BuscaArquivoGUI() {
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bPesquisar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        cSaida = new javax.swing.JTextArea();
        cCaminho = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        bLimpar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        bListarMelhores = new javax.swing.JButton();
        caixaLM = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        caixaLP = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        bListarPiores = new javax.swing.JButton();
        bLimparT = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Busca Arquivo");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        bPesquisar.setText("Pesquisar");
        bPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPesquisarActionPerformed(evt);
            }
        });

        cSaida.setColumns(20);
        cSaida.setEditable(false);
        cSaida.setRows(5);
        jScrollPane3.setViewportView(cSaida);

        cCaminho.setEditable(false);
        cCaminho.setToolTipText("");

        jLabel1.setText("Desenvolvido por: Érica Reis");
        jLabel1.setEnabled(false);

        bLimpar.setText("Limpar");
        bLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparActionPerformed(evt);
            }
        });

        bListarMelhores.setText("Listar");
        bListarMelhores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bListarMelhoresActionPerformed(evt);
            }
        });

        jLabel3.setText("Listar");

        jLabel4.setText("melhores casos");

        jLabel5.setText("Listar");

        jLabel6.setText("piores casos");

        bListarPiores.setText("Listar");
        bListarPiores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bListarPioresActionPerformed(evt);
            }
        });

        bLimparT.setText("Limpar tudo");
        bLimparT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLimparTActionPerformed(evt);
            }
        });

        jButton1.setText("Gerar .txt");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(bLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                    .addComponent(bLimparT, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                    .addComponent(jLabel5)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(caixaLP, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jLabel6)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                    .addComponent(bListarPiores, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGroup(layout.createSequentialGroup()
                                                    .addComponent(jLabel3)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(caixaLM, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(jLabel4)
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(bListarMelhores, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jButton1))
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cCaminho)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bPesquisar)))
                        .addGap(9, 9, 9)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bPesquisar)
                    .addComponent(cCaminho, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bLimpar)
                            .addComponent(bLimparT))
                        .addGap(18, 18, 18)
                        .addComponent(jButton1)
                        .addGap(63, 63, 63)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(caixaLM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bListarMelhores)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(caixaLP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bListarPiores)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(0, 84, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(5, 5, 5))
        );

        cCaminho.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPesquisarActionPerformed
            String caminho="";
            try {
                
                JFileChooser jfc=new JFileChooser();  
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  
                if(jfc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){  
                    caminho+=jfc.getSelectedFile().getAbsoluteFile().toString();
                    BuscaArquivo busca = new BuscaArquivo(caminho.trim());
                    cSaida.setText(busca.ordenaSaida());
                    cCaminho.setText(caminho);
                }
                
            }catch(CaminhoIncorreto e) {
                JOptionPane.showMessageDialog(null, "Por favor, digite um caminho válido", "Erro", JOptionPane.ERROR_MESSAGE);
            }catch(NaoEhDiretorio e){
                JOptionPane.showMessageDialog(null, "O caminho indicado nao corresponde a um diretorio válido", "Erro", JOptionPane.ERROR_MESSAGE);
            }catch(IOException e) {
                JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
            }
 
    }//GEN-LAST:event_bPesquisarActionPerformed

    private void bLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparActionPerformed
        cSaida.setText("");
    }//GEN-LAST:event_bLimparActionPerformed

    private void bListarMelhoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bListarMelhoresActionPerformed
        cSaida.setText("");
        caixaLP.setText("");
        try {    
            String saida="";
            int num;
            BuscaArquivo busca = new BuscaArquivo(cCaminho.getText().trim());
            num = Integer.parseInt(caixaLM.getText().trim());
            saida = busca.ordenaSaida();
            cSaida.setText(busca.listaMelhores(num, saida));
            
        }catch(CaminhoIncorreto e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um caminho válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(NaoEhDiretorio e) {
            JOptionPane.showMessageDialog(null, "O caminho indicado nao corresponde a um diretorio válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um valor válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }/*catch(ArquivoNaoExiste e) {
            JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
        }*/catch(IOException e) {
            JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(IndiceInvalido e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um valor válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }
            

    }//GEN-LAST:event_bListarMelhoresActionPerformed

    private void bListarPioresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bListarPioresActionPerformed
        cSaida.setText("");
        caixaLM.setText("");
        try {
            String saida="";
            int num;
            BuscaArquivo busca = new BuscaArquivo(cCaminho.getText().trim());
            num = Integer.parseInt(caixaLP.getText().trim());
            saida = busca.ordenaSaida();
            cSaida.setText(busca.listaPiores(num, saida));
        
        }catch(CaminhoIncorreto e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um caminho válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(NaoEhDiretorio e){
            JOptionPane.showMessageDialog(null, "O caminho indicado nao corresponde a um diretorio válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(IllegalArgumentException e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um valor válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }/*catch(ArquivoNaoExiste e) {
            JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
        }*/catch(IOException e) {
            JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
        }catch(IndiceInvalido e) {
            JOptionPane.showMessageDialog(null, "Por favor, digite um valor válido", "Erro", JOptionPane.ERROR_MESSAGE);
        }
            
    }//GEN-LAST:event_bListarPioresActionPerformed

    private void bLimparTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLimparTActionPerformed
        cSaida.setText("");
        cCaminho.setText("");
        caixaLM.setText("");
        caixaLP.setText("");
    }//GEN-LAST:event_bLimparTActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            try {
                BuscaArquivo busca = new BuscaArquivo(cCaminho.getText().trim());
                busca.geraConfig();
            }catch(CaminhoIncorreto e) {
                JOptionPane.showMessageDialog(null, "Por favor, digite um caminho válido", "Erro", JOptionPane.ERROR_MESSAGE);
            }catch(NaoEhDiretorio e){
                JOptionPane.showMessageDialog(null, "O caminho indicado nao corresponde a um diretorio válido", "Erro", JOptionPane.ERROR_MESSAGE);
            }catch(IOException e) {
                JOptionPane.showMessageDialog(null, "O arquivo não existe. Por favor, verifique o caminho dado", "Erro", JOptionPane.ERROR_MESSAGE);
            }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(BuscaArquivoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(BuscaArquivoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(BuscaArquivoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(BuscaArquivoGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new BuscaArquivoGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bLimpar;
    private javax.swing.JButton bLimparT;
    private javax.swing.JButton bListarMelhores;
    private javax.swing.JButton bListarPiores;
    private javax.swing.JButton bPesquisar;
    private javax.swing.JTextField cCaminho;
    private javax.swing.JTextArea cSaida;
    private javax.swing.JTextField caixaLM;
    private javax.swing.JTextField caixaLP;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
